#include "GameSystemManager.h"
#include "ApeExceptions.h"

Ogre::Timer GameSystemManager::timer;
GameSystemManager *GameSystemManager::mInstance = NULL;
InputHandler *GameSystemManager::mInput = NULL;
GUISystem *GameSystemManager::mCEGUI = NULL;
WindowEventHandler *GameSystemManager::mWindowHandler = NULL;
TerrainSystem *GameSystemManager::mTerrain = NULL;
OgreSystem *GameSystemManager::mOgre = NULL;
PhysicsSystem *GameSystemManager::mPhysics = NULL;
SkyGenerator *GameSystemManager::mSky = NULL;
PagingSystem *GameSystemManager::mPaging = NULL;
XMLHandler *GameSystemManager::mXML = NULL;

//RakNetClient *GameSystemManager::mRakNetClient = NULL;
//InputSystem *GameSystemManager::mInput = NULL;
	
GameSystemManager::GameSystemManager()
{
	startTime = timer.getMilliseconds();
}

GameSystemManager::~GameSystemManager()
{ 

}

GameSystemManager* GameSystemManager::getSingleton() {
	if(mInstance == NULL)
		mInstance = new GameSystemManager();
	return mInstance;
}

void GameSystemManager::updateTimer() {
	
	finishTime = timer.getMilliseconds();
	elapsedTime = (finishTime - startTime ) / 1000;
	startTime = timer.getMilliseconds();
}

Ogre::Real GameSystemManager::getTime() {
	return elapsedTime;
}


//Ogre::Real GameSystemManager::getElapsedTime() {
//	return elapsedTime;
//}

void GameSystemManager::createOgre() {
	mOgre = new OgreSystem();
}

OgreSystem* GameSystemManager::getOgre() {
	return mOgre;
}

/*
void GameSystemManager::startAllSystems() {
	createTerrain();
	createPhysics();
	createGUI();
	createNetwork();
}

*/

void GameSystemManager::createInput() {
	mInput = new InputHandler(getOgre()->getWindowHandler());
}
InputHandler* GameSystemManager::getInput() {
	return mInput;
}

void GameSystemManager::createGUI() {
	mCEGUI = new GUISystem();
	//mCEGUI->UpdateState();
}
GUISystem* GameSystemManager::getGUI() {
	return mCEGUI;
}

void GameSystemManager::createWindowHandler() {
	mWindowHandler = new WindowEventHandler();
	//mWindowHandler->UpdateState();
}
WindowEventHandler* GameSystemManager::getWindowHandler() {
	return mWindowHandler;
}

void GameSystemManager::createTerrain() {
	if(!mTerrain)
		mTerrain = new TerrainSystem();
	//mTerrain->generateTerrain();
	//mTerrain->loadTerrain();
}

TerrainSystem* GameSystemManager::getTerrain() {
	return mTerrain;
}

void GameSystemManager::createXMLHandler() {
	mXML = new XMLHandler();
}
XMLHandler* GameSystemManager::getXMLHandler() {
	return mXML;
}

void GameSystemManager::initSky() {
	mSky = new SkyGenerator();
}
SkyGenerator* GameSystemManager::getSky() {
	return mSky;
}

void GameSystemManager::createPaging() {
	mPaging = new PagingSystem();
}
PagingSystem* GameSystemManager::getPaging() {
	return mPaging;
}

void GameSystemManager::createPhysics() {
	mPhysics = new PhysicsSystem();
	//initializePhysx();
}

PhysicsSystem* GameSystemManager::getPhysics() {
	return mPhysics;
}
/*
bool GameSystemManager::groupExists( Ogre::String &group ) {
   Ogre::StringVector groupNameList = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
   Ogre::StringVector::iterator resGroupIter = groupNameList.begin();
	
   Ogre::ResourceManager::ResourceMapIterator matMap = Ogre::ResourceGroupManager::getSingleton().getres

   for(;resGroupIter < groupNameList.end();resGroupIter++)
   {
      Ogre::String resGroupName = (*resGroupIter);
      if( resGroupName == group )
      {
				
             return true;
       }
   }
   return false;
}
*/